﻿using SD.Common;
using SD.Infrastructure.EntityBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SlamDunk.MES.Domain.Entities.AcquisitionContext
{
    /// <summary>
    /// 采集规则
    /// </summary>
    public class AcquisitionRule : AggregateRootEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected AcquisitionRule()
        {
            //初始化导航属性
            this.Items = new HashSet<AcquisitionRuleItem>();
        }
        #endregion

        #region 01.创建采集规则构造器
        /// <summary>
        /// 创建采集规则构造器
        /// </summary>
        /// <param name="ruleNo">采集规则编号</param>
        /// <param name="ruleName">采集规则名称</param>
        /// <param name="processId">工艺Id</param>
        /// <param name="processSegmentId">工艺段Id</param>
        /// <param name="interval">采集间隔（单位：秒）</param>
        /// <param name="description">描述</param>
        public AcquisitionRule(string ruleNo, string ruleName, Guid? processId, Guid? processSegmentId, int interval, string description)
            : this()
        {
            base.Number = ruleNo;
            base.Name = ruleName;
            this.ProcessId = processId;
            this.ProcessSegmentId = processSegmentId;
            this.Interval = interval;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #endregion

        #region # 属性

        #region 工艺Id —— Guid? ProcessId
        /// <summary>
        /// 工艺Id
        /// </summary>
        public Guid? ProcessId { get; private set; }
        #endregion

        #region 工艺段Id —— Guid? ProcessSegmentId
        /// <summary>
        /// 工艺段Id
        /// </summary>
        public Guid? ProcessSegmentId { get; private set; }
        #endregion

        #region 采集间隔 —— int Interval
        /// <summary>
        /// 采集间隔（单位：秒）
        /// </summary>
        public int Interval { get; private set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; private set; }
        #endregion

        #region 导航属性 - 采集规则项集 —— ICollection<AcquisitionRuleItem> Items
        /// <summary>
        /// 导航属性 - 采集规则项集
        /// </summary>
        public virtual ICollection<AcquisitionRuleItem> Items { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 修改采集规则 —— void UpdateInfo(string ruleNo, string ruleName...
        /// <summary>
        /// 修改采集规则
        /// </summary>
        /// <param name="ruleNo">采集规则编号</param>
        /// <param name="ruleName">采集规则名称</param>
        /// <param name="processId">工艺Id</param>
        /// <param name="processSegmentId">工艺段Id</param>
        /// <param name="interval">采集间隔（单位：秒）</param>
        /// <param name="description">描述</param>
        public void UpdateInfo(string ruleNo, string ruleName, Guid? processId, Guid? processSegmentId, int interval, string description)
        {
            base.Number = ruleNo;
            base.Name = ruleName;
            this.ProcessId = processId;
            this.ProcessSegmentId = processSegmentId;
            this.Interval = interval;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #region 设置采集规则项 —— void SetItems(IEnumerable<AcquisitionRuleItem>...
        /// <summary>
        /// 设置采集规则项
        /// </summary>
        /// <param name="ruleItems">采集规则项集</param>
        public void SetItems(IEnumerable<AcquisitionRuleItem> ruleItems)
        {
            ruleItems = ruleItems?.ToArray() ?? new AcquisitionRuleItem[0];

            this.ClearItems();
            if (ruleItems.Any())
            {
                this.Items.AddRange(ruleItems);
                foreach (AcquisitionRuleItem ruleItem in ruleItems)
                {
                    ruleItem.AcquisitionRule = this;
                }
            }
        }
        #endregion

        #region 清空采集规则项 —— void ClearItems()
        /// <summary>
        /// 清空采集规则项
        /// </summary>
        private void ClearItems()
        {
            foreach (AcquisitionRuleItem ruleItem in this.Items.ToArray())
            {
                this.Items.Remove(ruleItem);
            }
        }
        #endregion

        #region 初始化关键字 —— void InitKeywords()
        /// <summary>
        /// 初始化关键字
        /// </summary>
        private void InitKeywords()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(base.Number);
            builder.Append(base.Name);
            builder.Append(this.Description);

            base.SetKeywords(builder.ToString());
        }
        #endregion

        #endregion
    }
}
